package service.impl;

import dao.AccountDao;
import dao.impl.AccountDaoImpl;
import entity.Account;
import service.AccountService;
import utils.InfoUtils;

/**
 * @program: FinalExam
 * @description:
 * @create: 2021-12-14 17:13
 * @Date 2021/12/14
 **/

public class AccountServiceImpl implements AccountService {
    private AccountDao accountDao = new AccountDaoImpl();


    public Account QueryMyBalance(String user_id) {
        Account account = accountDao.QueryMyBalance(user_id);
        return account;
    }

    /**
     * 根据卡号转账
     * @param id 转账人的id
     * @param money 转账金额
     * @param username 给谁转账
     * @return
     */
    public Boolean transferAccountsNyUsername(String id ,Double money, String username) {
        // TODO　先进行查询 看余额是否足够 然后对两个人的金额进行增加或者减少
        Account giveAccount = accountDao.QueryMyBalance(id);
        Account toAccount = accountDao.QueryMyBalanceByUsername(username);
        accountDao.transferAccountsNyUsername(giveAccount.getBalance()-money,id);
        accountDao.transferAccountsNyUsername(toAccount.getBalance()+money,username);
        return true;
    }

    public Boolean withdrawalById(String id, Double money) {
        return null;
    }

    public Account QueryMyBalanceByUsername(String toUsername) {
        return accountDao.QueryMyBalanceByUsername(toUsername);
    }

    public void saveMoney(Double money, String uid) {
        accountDao.saveMoney(money, uid);
    }

    public void deleteAccount(String user_id) {
        String gmt_modify = InfoUtils.getTime();
        accountDao.deleteAccount(user_id, gmt_modify);
    }
}
